/**
 *  功能：DS18B20 温度监控配置
 *  作者：tuut
 *  时间：2023-11-28
 *  描述：
 *      1.设备初始化
 *      2.设备周边温度读取
 *      3.测试官方方法的使用
*/
#include "Sketch_oct31b_ds18b20.h"  // DS18B20 设备引用 温度检测设备
#include <Arduino.h>
/****************************************DS18B20 part****************************************/
Sketch_oct31b_ds18b20::Sketch_oct31b_ds18b20() {
  Serial.println("Sketch_oct31b_ds18b20 Object Created.");
}

Sketch_oct31b_ds18b20::Sketch_oct31b_ds18b20(int one_wire_bus_pin) {
  Serial.println("Sketch_oct31b_ds18b20 Object Created.");
  ONE_WIRE_BUS_PIN = one_wire_bus_pin;
}

Sketch_oct31b_ds18b20::~Sketch_oct31b_ds18b20() {
  Serial.println("Sketch_oct31b_ds18b20 Object Deleted.");
}

int Sketch_oct31b_ds18b20::getPin() {
  return ONE_WIRE_BUS_PIN;
}

/**
 * 功能描述：初始化DS18B20温度探测棒
 */
void Sketch_oct31b_ds18b20::initDS18b60DevManager() {
  // ONE_WIRE_BUS_PIN = 14;
  oneWire = OneWire(ONE_WIRE_BUS_PIN);
  // 注册一个Ds18b20传感器
  sensors = DallasTemperature(&oneWire);
  cal_delayStart = millis();
  Serial.println("Dallas Temperature IC Control Library Demo");
  // 搜索本地DS18b20设备
  Serial.print("本地设备...");
  // Start up the library
  sensors.begin();
  Serial.print("找到 ");
  Serial.print(sensors.getDeviceCount(), DEC);
  Serial.println(" 设备.");
  // 报告DS18b20设备电源状态
  Serial.print("电源状态: ");
  if (sensors.isParasitePowerMode()) Serial.println("打开");
  else Serial.println("关闭");
  if (!sensors.getAddress(insideThermometer, 0)) {
    Serial.println("不能找到设备[0]地址");
  } else {
    // 显示总线上设备0的地址
    Serial.print("设备 [0] 地址: ");
    printAddress(insideThermometer);
    Serial.println();
    // set the resolution to 9 bit per device
    sensors.setResolution(insideThermometer, TEMPERATURE_PRECISION);
    Serial.print("Device 0 Resolution: ");
    Serial.print(sensors.getResolution(insideThermometer), DEC);
    Serial.println();
  }
  if (!sensors.getAddress(outsideThermometer, 1)) {
    Serial.println("不能找到设备[1]地址");
  } else {
    // 显示总线上设备1的地址
    Serial.print("设备 [1] 地址: ");
    printAddress(outsideThermometer);
    Serial.println();
    // set the resolution to 9 bit per device
    sensors.setResolution(outsideThermometer, TEMPERATURE_PRECISION);
    Serial.print("Device 1 Resolution: ");
    Serial.print(sensors.getResolution(outsideThermometer), DEC);
    Serial.println();
  }
  // call sensors.requestTemperatures() to issue a global temperature
  // request to all devices on the bus
  Serial.print("Requesting temperatures...");
  sensors.requestTemperatures();
  Serial.println("DONE");
}

void Sketch_oct31b_ds18b20::loadDS18B20API(long _interval) {
  if (millis() - cal_delayStart >= _interval) {
    Serial.println("");
    Serial.print("outsideTemperature:");
    str_outsideTemp = String(printData(outsideThermometer), 1);
    Serial.print(str_outsideTemp);
    Serial.print("insideTemperature:");
    str_insideTemp = String(printData(insideThermometer), 1);
    Serial.print(str_insideTemp);
    Serial.println("");
    cal_delayStart = millis();
  }
}

/*
 * Main function, get and show the temperature
 */
float Sketch_oct31b_ds18b20::printData(DeviceAddress deviceAddress) {
  Serial.print("Device Address: ");
  printAddress(deviceAddress);
  Serial.print(" ");
  return printTemperature(deviceAddress);
}

/**
 * 功能描述：function to print a device address
 */
void Sketch_oct31b_ds18b20::printAddress(DeviceAddress deviceAddress) {
  for (uint8_t i = 0; i < 8; i++) {
    if (deviceAddress[i] < 16) Serial.print("0");
    Serial.print(deviceAddress[i], HEX);
  }
}

// function to print the temperature for a device
float Sketch_oct31b_ds18b20::printTemperature(DeviceAddress deviceAddress) {
  // method 1 - slower
  //Serial.print("Temp C: ");
  //Serial.print(sensors.getTempC(deviceAddress));
  //Serial.print(" Temp F: ");
  //Serial.print(sensors.getTempF(deviceAddress)); // Makes a second call to getTempC and then converts to Fahrenheit
  // method 2 - faster
  float tempC = sensors.getTempC(deviceAddress);
  if (tempC == DEVICE_DISCONNECTED_C) {
    // Serial.println("Error: Could not read temperature data");
    return 0;
  }
  Serial.print("Temp C: ");
  Serial.print(tempC);
  Serial.print(" Temp F: ");
  Serial.println(DallasTemperature::toFahrenheit(tempC));  // Converts tempC to Fahrenheit
  return tempC;
}

float Sketch_oct31b_ds18b20::getDs18b20Temp() {
  float tempC = sensors.getTempC(insideThermometer);
  if (tempC == DEVICE_DISCONNECTED_C) {
    // Serial.println("Error: Could not read temperature data");
    return 0;
  }
  tempC = sensors.getTempC(insideThermometer);
  return tempC;
}